<?php
namespace app\madmin\controller;
use app\madmin\model\MallConfsModel;

/**
 * 商品配置控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class MallConfs extends PublicBase{

	# 商品快捷配置方法
	public function showBase(){
        $MallConfs = new MallConfsModel;

		if(request()->isPost()){
            $Config = input('post.');

            if(!empty($Config)){
                # 循环更新配置
                foreach($Config as $k => $v){
                    $Where = [];
                    $Where['confs_name'] = $k;
                    $Where['confs_show'] = 1;
                    $FindConfig = $MallConfs->mInfo($Where);
                    if(!empty($FindConfig)){
                        if($FindConfig['confs_value'] != $v){
                            $MallConfs->mUpdate($Where, ['confs_value'=>$v]);
                        }
                    }
                }
            }

            # 更新配置缓存
            $this->MallConfigLists();

            $this->ShowData(1, lang('XtTsGxCg'), url('MallConfs/showBase'));
        }else{
            # 获取配置栏目
            $ListGroup = $this->getLexiconCache('MALL_CONFIG_GROUP');

            $ListLists = $MallConfs->mAll(['confs_show'=>1]);

            $Group = [];
            $Lists = [];
            if(!empty($ListGroup)){
                # 过滤配置空闲栏目
                foreach($ListGroup as $k => $v){
                    $GroupCount = $MallConfs->mCount(['confs_group'=>$v['content_value'], 'confs_show'=>1]);
                    if($GroupCount > 0){
                        $Group[$k]['group_type'] = $v['content_value'];
                        $Group[$k]['group_name'] = $v['content_name'];
                    }
                }

                # 循环配置添加分组
                foreach($Group as $k => $v){
                    $Lists[$k] = $v;
                    foreach($ListLists as $i => $z){
                        if($v['group_type'] == $z['confs_group']){
                            if($z['confs_type'] == 2){
                                if(!empty($z['confs_select'])){
                                    $ListLists[$i]['confs_select'] = explode(',', $z['confs_select']);
                                }
                            }
                            $Lists[$k]['group_list'][] = $z;
                            unset($z);
                        }
                    }
                }
            }

            # 模版赋值
            return $this->fetch('MallConfs/showBase', [
                'Group' => $Group,
                'Lists' => $Lists
            ]);
        }
	}

    # 商品配置列表方法
    public function showList(){
        $MallConfs = new MallConfsModel;

        $Where = $this->getFormWhere();

        $confs_group = input('param.confs_group');
        if(!empty($confs_group)){
            $Where['confs_group'] = $confs_group;
        }
        $this->assign('confs_group', $confs_group);

        $confs_type = input('param.confs_type');
        if(!empty($confs_type)){
            $Where['confs_type'] = $confs_type;
        }
        $this->assign('confs_type', $confs_type);

        $Lists = $MallConfs->mList($Where, '*', config('CNF_LISTPAGE'));

        # 模版赋值
        return $this->fetch('MallConfs/showList', [
            'Lists' => $Lists,
            'Group' => $this->getLexiconCache('MALL_CONFIG_GROUP')
        ]);
    }

    # 商品配置新增方法
    public function showAdd(){
        $MallConfs = new MallConfsModel;

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $MallConfs->mAdd($Param);

            # 更新配置缓存
            $this->MallConfigLists();

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            # 模版赋值
            return $this->fetch('MallConfs/showAdd', [
                'Info'  => Null,
                'Group' => $this->getLexiconCache('MALL_CONFIG_GROUP')
            ]);
        }
    }

    # 商品配置更新方法
    public function showEdit(){
        $MallConfs = new MallConfsModel;

        $ID = input('param.id');

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $MallConfs->mEdit(['confs_id'=>$ID], $Param);

            # 更新配置缓存
            $this->MallConfigLists();

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            $Info = $MallConfs->mInfo(['confs_id'=>$ID]);

            if(empty($Info)){
                $this->ShowData(2, lang('XtCwCxCw'));
            }else{
                # 模版赋值
                return $this->fetch('MallConfs/showAdd', [
                    'Info'  => $Info,
                    'Group' => $this->getLexiconCache('MALL_CONFIG_GROUP')
                ]);
            }
        }
    }

    # 商品配置删除方法
    public function showDel(){
        if(request()->isPost()){
            $MallConfig = new MallConfsModel;

            $ID = input('post.id');

            $Back = $MallConfig->mDel(['confs_id'=>$ID]);

            # 更新配置缓存
            $this->MallConfigLists();

            $this->ShowData($Back['code'], $Back['msgs']);
        }
    }

}